package com.chookss.mine.personal;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.chookss.R;
import com.chookss.Urls;
import com.chookss.mine.entity.MyViedeoEntity;
import com.chookss.tools.MyToast;
import com.chookss.tools.SignUtils2;
import com.chookss.tools.Utils;
import com.chookss.tools.okgoconfig.MyHttpRequest;
import com.chookss.tools.okgoconfig.callback.ObserverCallback;
import com.chookss.tools.okgoconfig.model.LzyResponse;
import com.chookss.view.RecycleViewDivider;
import com.google.gson.reflect.TypeToken;
import com.lvgroup.hospital.base.BaseAct;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.annotations.Nullable;

public class VideoSetEditActivity extends BaseAct {


    @BindView(R.id.common_title_txt)
    TextView commonTitleTxt;
    @BindView(R.id.common_right_txt)
    TextView commonRightTxt;
    @BindView(R.id.tvContent)
    EditText tvContent;
    private int index = -1;//
    private String collectionsCode,collectionsName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        setContentView(R.layout.activity_video_set_edit);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        initState();
        commonTitleTxt.setText("我的集合");
        commonRightTxt.setText("确定");
        commonRightTxt.setVisibility(View.VISIBLE);
        collectionsCode=getIntent().getStringExtra("collectionsCode");
        collectionsName=getIntent().getStringExtra("collectionsName");
        index=getIntent().getIntExtra("index",0);
        tvContent.setText(collectionsName);
        if(!Utils.isNull(collectionsName)){
            tvContent.setSelection(collectionsName.length());
        }
        tvContent.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
             if(s.toString().length()>19){
                 MyToast.show("最多输入20个字");
             }
            }
        });
        Utils.showSoftInputFromWindow(this, tvContent);
    }

    private void updateCollection() {
        Map<String, String> allmap = new HashMap<>();
        allmap.put("collectionsCode", collectionsCode);
        allmap.put("collectionsName", tvContent.getText().toString());
        MyHttpRequest.getDefault().postRequest(new TypeToken<LzyResponse<String>>() {
        }.getType(), null, Urls.updateCollection, SignUtils2.makeSignStr(allmap), false, new ObserverCallback<String>() {
            @Override
            public void onSuccess(String data) {
                MyToast.show("修改成功");
                Intent intent=new Intent();
                intent.putExtra("collectionsName", tvContent.getText().toString());
                intent.putExtra("index", index);
                setResult(1, intent);
                finish();
            }

            @Override
            public void onError(String errorMsg) {
                MyToast.show(Utils.onErrorTips(errorMsg));
            }
        });
    }

    @OnClick({R.id.common_title_back, R.id.common_right_txt})
    public void onClick(View view) {

        switch (view.getId()) {
            case R.id.common_title_back:
                finish();
                break;
            case R.id.common_right_txt:
                updateCollection();
                break;
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(tvContent!=null){
            Utils.hideInputKeyboard(VideoSetEditActivity.this, tvContent);
        }
    }
}
